import SwiftUI

struct LoadingView: View {
    let message: String
    let progress: Double?
    let totalPhotos: Int?
    let currentPhoto: Int?
    
    @State private var isAnimating = false
    
    init(message: String = "正在加载照片...", progress: Double? = nil, totalPhotos: Int? = nil, currentPhoto: Int? = nil) {
        self.message = message
        self.progress = progress
        self.totalPhotos = totalPhotos
        self.currentPhoto = currentPhoto
    }
    
    var body: some View {
        VStack(spacing: 30) {
            Spacer()
            
            // 加载动画
            ZStack {
                // 外圈
                Circle()
                    .stroke(Color.blue.opacity(0.2), lineWidth: 8)
                    .frame(width: 120, height: 120)
                
                // 旋转的进度圈
                Circle()
                    .trim(from: 0, to: progress ?? 0.3)
                    .stroke(Color.blue, style: StrokeStyle(lineWidth: 8, lineCap: .round))
                    .frame(width: 120, height: 120)
                    .rotationEffect(.degrees(-90))
                    .animation(.easeInOut(duration: 1), value: progress)
                
                // 中心图标
                Image(systemName: "photo.fill")
                    .font(.system(size: 40))
                    .foregroundColor(.blue)
                    .scaleEffect(isAnimating ? 1.1 : 1.0)
                    .animation(.easeInOut(duration: 0.8).repeatForever(autoreverses: true), value: isAnimating)
            }
            
            // 消息文字
            VStack(spacing: 12) {
                Text(message)
                    .font(.title2)
                    .fontWeight(.medium)
                    .multilineTextAlignment(.center)
                
                if let progress = progress {
                    Text("\(Int(progress * 100))%")
                        .font(.headline)
                        .foregroundColor(.blue)
                }
                
                if let totalPhotos = totalPhotos, let currentPhoto = currentPhoto {
                    Text("已处理 \(currentPhoto) / \(totalPhotos) 张照片")
                        .font(.caption)
                        .foregroundColor(.secondary)
                }
            }
            
            // 进度条
            if let progress = progress {
                ProgressView(value: progress)
                    .progressViewStyle(LinearProgressViewStyle(tint: .blue))
                    .frame(width: 200)
            }
            
            // 详细说明
            VStack(spacing: 8) {
                LoadingStepRow(
                    icon: "photo.fill",
                    title: "读取照片",
                    description: "从您的相册中读取所有照片"
                )
                
                LoadingStepRow(
                    icon: "location.fill",
                    title: "提取位置信息",
                    description: "获取照片中的地理位置数据"
                )
                
                LoadingStepRow(
                    icon: "calendar",
                    title: "时间轴整理",
                    description: "按时间顺序组织您的照片"
                )
                
                LoadingStepRow(
                    icon: "sparkles",
                    title: "智能分析",
                    description: "分析照片内容并生成回忆"
                )
            }
            .padding(.horizontal, 40)
            
            Spacer()
        }
        .padding()
        .onAppear {
            isAnimating = true
        }
    }
}

struct LoadingStepRow: View {
    let icon: String
    let title: String
    let description: String
    @State private var isCompleted = false
    
    var body: some View {
        HStack(spacing: 12) {
            Image(systemName: isCompleted ? "checkmark.circle.fill" : icon)
                .font(.title3)
                .foregroundColor(isCompleted ? .green : .blue)
                .frame(width: 24)
            
            VStack(alignment: .leading, spacing: 2) {
                Text(title)
                    .font(.subheadline)
                    .fontWeight(.medium)
                    .foregroundColor(isCompleted ? .green : .primary)
                
                Text(description)
                    .font(.caption)
                    .foregroundColor(.secondary)
            }
            
            Spacer()
        }
        .opacity(isCompleted ? 0.7 : 1.0)
        .animation(.easeInOut(duration: 0.5), value: isCompleted)
    }
}

// MARK: - 照片处理进度视图

struct PhotoProcessingView: View {
    @ObservedObject var photoService: PhotoService
    @State private var currentStep = 0
    
    private let steps = [
        ("photo.fill", "读取照片", "从相册中获取所有照片"),
        ("location.fill", "提取位置", "获取照片的地理位置信息"),
        ("calendar", "时间整理", "按时间顺序组织照片"),
        ("sparkles", "智能分析", "分析照片内容并生成回忆")
    ]
    
    var body: some View {
        VStack(spacing: 30) {
            Spacer()
            
            // 进度指示器
            VStack(spacing: 20) {
                ForEach(0..<steps.count, id: \.self) { index in
                    ProcessingStepView(
                        icon: steps[index].0,
                        title: steps[index].1,
                        description: steps[index].2,
                        isActive: index == currentStep,
                        isCompleted: index < currentStep
                    )
                }
            }
            
            // 照片统计
            if !photoService.photos.isEmpty {
                VStack(spacing: 12) {
                    Text("已加载 \(photoService.photos.count) 张照片")
                        .font(.headline)
                        .foregroundColor(.blue)
                    
                    if let statistics = getPhotoStatistics() {
                        VStack(spacing: 8) {
                            Text("📍 来自 \(statistics.totalLocations) 个地点")
                            Text("📅 时间跨度: \(statistics.dateRangeText)")
                            if let mostFrequent = statistics.mostFrequentLocation {
                                Text("🏆 最常去: \(mostFrequent.displayName)")
                            }
                        }
                        .font(.caption)
                        .foregroundColor(.secondary)
                    }
                }
                .padding()
                .background(Color.blue.opacity(0.1))
                .cornerRadius(12)
            }
            
            Spacer()
        }
        .padding()
        .onAppear {
            startProcessingAnimation()
        }
    }
    
    private func startProcessingAnimation() {
        // 模拟处理步骤
        for step in 0..<steps.count {
            DispatchQueue.main.asyncAfter(deadline: .now() + Double(step) * 1.5) {
                withAnimation(.easeInOut(duration: 0.5)) {
                    currentStep = step + 1
                }
            }
        }
    }
    
    private func getPhotoStatistics() -> PhotoStatistics? {
        guard !photoService.photos.isEmpty else { return nil }
        return PhotoStatistics(photos: photoService.photos)
    }
}

struct ProcessingStepView: View {
    let icon: String
    let title: String
    let description: String
    let isActive: Bool
    let isCompleted: Bool
    
    var body: some View {
        HStack(spacing: 12) {
            // 状态图标
            ZStack {
                Circle()
                    .fill(backgroundColor)
                    .frame(width: 40, height: 40)
                
                Image(systemName: statusIcon)
                    .font(.system(size: 18, weight: .medium))
                    .foregroundColor(statusColor)
            }
            
            VStack(alignment: .leading, spacing: 4) {
                Text(title)
                    .font(.headline)
                    .foregroundColor(textColor)
                
                Text(description)
                    .font(.caption)
                    .foregroundColor(.secondary)
            }
            
            Spacer()
        }
        .opacity(isCompleted ? 0.7 : 1.0)
        .scaleEffect(isActive ? 1.05 : 1.0)
        .animation(.easeInOut(duration: 0.3), value: isActive)
    }
    
    private var statusIcon: String {
        if isCompleted {
            return "checkmark"
        } else if isActive {
            return icon
        } else {
            return icon
        }
    }
    
    private var statusColor: Color {
        if isCompleted {
            return .white
        } else if isActive {
            return .white
        } else {
            return .blue
        }
    }
    
    private var backgroundColor: Color {
        if isCompleted {
            return .green
        } else if isActive {
            return .blue
        } else {
            return .blue.opacity(0.2)
        }
    }
    
    private var textColor: Color {
        if isCompleted {
            return .green
        } else if isActive {
            return .blue
        } else {
            return .primary
        }
    }
}

// MARK: - 扩展 PhotoStatistics

// 此扩展已在 PhotoData.swift 中定义，这里移除避免重复。

#Preview {
    LoadingView(message: "正在加载照片...", progress: 0.6, totalPhotos: 1000, currentPhoto: 600)
} 